<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="auth" uri="http://www.jiyinit.com/authTag"%>

<div class="right-container">
    <div class="container-select text-left">
        <div class="search-input">
            <span>登录名：</span>
            <input id="loginName" name="loginName" type="text">
        </div>
        <div class="search-input">
            <span>用户姓名：</span>
            <input id="userName" name="userName" type="text">
        </div>
        <div class="search-input">
            <span>部门名称：</span>
            <input id="orgName" name="orgName" type="text">
        </div>

        <button id="searchBtn" class="btn btn-primary" type="button">
            <i class="fa fa-search"></i>&nbsp;查询
        </button>
    </div>
    <div class="manage-button text-left">
        <auth:auth authUrl="user/save">
            <button id="addBtn" class="btn btn-primary" type="button">
                <i class="fa fa-plus"></i>&nbsp;新增
            </button>
        </auth:auth>

        <auth:auth authUrl="user/modify">
            <button id="modifyBtn" class="btn btn-primary" type="button">
                <i class="fa fa-pencil"></i>&nbsp;修改
            </button>
        </auth:auth>

        <auth:auth authUrl="user/delete">
            <button id="deleteBtn" class="btn btn-primary" type="button">
                <i class="fa fa-trash-o"></i>&nbsp;删除
            </button>
        </auth:auth>

        <auth:auth authUrl="user/modifyPassword">
            <button id="modifyPasswordBtn" class="btn btn-primary" type="button">
                <i class="fa fa-key"></i>&nbsp;修改密码
            </button>
        </auth:auth>
    </div>
    <div class="row">
        <div class="col-xs-12">

            <table id="grid-table"></table>

            <div id="grid-pager"></div>

        </div>
    </div>
</div>

<!-- page specific plugin scripts -->
<script src="${basePath}/plugins/jqGrid/jquery.jqGrid.min.js${VERSION}"></script>
<script src="${basePath}/plugins/jqGrid/grid.locale-cn.js${VERSION}"></script>

<!-- inline scripts related to this page -->
<script type="text/javascript">
    var pageii = null;
    jQuery(function ($) {
      /*  CommonUtils.timeout("#error");*/
        CommonUtils.grid({
            url: "${basePath}/user/findByPage",
            colNames: ['序号','登录名', '用户姓名', '手机号码', '部门名称','创建时间'],
            colModel: [
                {name: 'userId', index: 'id', key: true, width: 30, sorttype: "int"},
                {
                    name: 'loginName',
                    index: 'loginName',
                    width: 50
                },
                {
                    name: 'userName',
                    index: 'userName',
                    width: 50
                },
                {
                    name: 'userMobileNo',
                    index: 'userMobileNo',
                    width: 100
                },
                {
                    name: 'orgName',
                    index: 'orgName',
                    width: 100
                },
                {
                    name: 'createTime',
                    index: 'createTime',
                    width: 90,
                    formatter: CommonUtils.formateDate
                }
            ],
            jsonReader: {
                root: "rows", page: "currentPage", total: "totalPages",
                records: "totalRecords", repeatitems: false, id: "id"
            }
        });

        $("#searchBtn").click(function () {
            query();
        });

     /*   $("#userName").keypress(function (e) {
            if (e.which == 13) {
                query();
            }
        });*/

        function query() {
            var queryData = {
                "userName": $.trim($('#userName').val()),
                "loginName": $.trim($('#loginName').val()),
                "orgName": $.trim($('#orgName').val())
            };

            $("#grid-table").jqGrid("setGridParam",
                    {
                        postData: queryData,
                        datatype: 'json'
                    }
            ).trigger("reloadGrid", [{page: 1}]);
        };

        $("#addBtn").click("click", function () {
            if ($(window).width() > 800){
                pageii = layer.open({
                    title: "新增用户",
                    type: 2,
                    maxmin: true,
                    area: ["800px", "90%"],
                    content: ['${basePath}/user/add']
                });
            }
            else{
                pageii = layer.open({
                    title: "新增用户",
                    type: 2,
                    maxmin: true,
                    area: ["80%", "90%"],
                    content: ['${basePath}/user/add']
                });
            }


        });



        $("#modifyBtn").click("click", function () {

            var rowId = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
            if (rowId.length > 1 || rowId == "") {
                layer.msg("请选择一行数据进行修改！");
                return;
            }
            var rowData = $("#grid-table").jqGrid('getRowData', rowId);
            var id = rowData.userId;

            pageii = layer.open({
                title: "修改用户信息",
                type: 2,
                area: ["800px", "500px"],
                content: '${basePath}/user/edit?userId=' + id
            });

        });

        var loading = null;

        $('#deleteBtn').click("click", function () {
            var rowsIds = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
            if (rowsIds.length <= 0  || rowsIds == "") {
                layer.msg("请至少选择一行数据进行删除！");
                return;
            }
            layer.confirm('是否删除选中数据？',{ icon: 3}, function (index) {
                var userIds = [];
                $(rowsIds).each(function () {
                    var rowData = $("#grid-table").jqGrid('getRowData', this);
                    userIds.push(rowData.userId);
                });

                $.ajax({
                    type: "post",
                    data: "userIds=" + userIds,
                    url: "${basePath}/user/delete",
                    dataType: "json",
                    beforeSend:function () {
                        $('#deleteBtn').prop("disabled", true);
                        loading = layer.load(0, {
                            shade: [0.4,'#fff'] //0.4透明度的白色背景
                        });
                    },
                    success: function (data) {
                        $('#deleteBtn').prop("disabled", false);
                        layer.close(loading);
                        if (data.status == "0") {
                            layer.msg("删除成功！");
                            $("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
                        }
                        else {
                            layer.alert(data, {
                                icon: 0,
                                skin: 'layui-layer-lan',
                                closeBtn: 0
                            })
                        }
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        $('#deleteBtn').prop("disabled", false);
                        layer.close(loading);
                    }
                });
                return false;

            });

        });

        $("#modifyPasswordBtn").click("click", function () {
            var rowId = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
            if (rowId.length > 1 || rowId == "") {
                layer.msg("请选择一行数据进行修改！");
                return;
            }
            var rowData = $("#grid-table").jqGrid('getRowData', rowId);
            var id = rowData.userId;

            pageii = layer.open({
                title: "修改密码",
                type: 2,
                area: ["800px", "500px"],
                content: '${basePath}/user/toModifyPassword?userId=' + id
            });
        });
    });
</script>
